﻿using System;
using System.Collections.Generic;

namespace Indigox.CMS.Administration
{
    public class Schema
    {
        private static Schema m_default = new Schema();

        public static Schema Default
        {
            get { return Schema.m_default; }
        }

        internal Dictionary<string, ContentType> ContentTypes = new Dictionary<string, ContentType>();
        internal WriteLock WriteLock = new WriteLock();

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public ContentType[] GetContentTypes()
        {
            ContentType[] array = null;
            WriteLock.Wait( () =>
            {
                array = new ContentType[ ContentTypes.Count ];
                ContentTypes.Values.CopyTo( array, 0 );
            } );
            return array;
        }

        public ContentType GetContentType( string name )
        {
            ContentType contentType = null;
            WriteLock.Wait( () =>
            {
                if ( ContentTypes.ContainsKey( name ) )
                {
                    contentType = ContentTypes[ name ];
                }
            } );

            if ( contentType == null )
                throw new NullReferenceException( "找不到名称为" + name + "的内容类型" );

            return contentType;
        }
    }
}
